package org.ellipticcurve;

import java.math.BigInteger;
import java.security.*;
import java.security.spec.ECFieldFp;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.EllipticCurve;

import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
import org.bouncycastle.jce.ECPointUtil;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public class Example {

	/**
	 * @param args
	 * @throws NoSuchProviderException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidAlgorithmParameterException
	 */
	public static void Exmain(String[] args) throws NoSuchAlgorithmException,
			NoSuchProviderException, InvalidAlgorithmParameterException {

		BigInteger a = new BigInteger(
				"7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
				16);

		System.out.println("a=" + a);

		BigInteger q = new BigInteger(
				"883423532389192164791648750360308885314476597252960362792450860609699839");
		System.out.println("q=" + q);
		BigInteger b = new BigInteger(
				"6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",
				16);
		System.out.println("b=" + b);
		EllipticCurve curve = new EllipticCurve(new ECFieldFp(q), a, b); // b

		ECPoint G = ECPointUtil
				.decodePoint(
						curve,
						Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf"));

		System.out.println("G.X=" + G.getAffineX());
		System.out.println("G.Y=" + G.getAffineY());

		BigInteger n = new BigInteger(
				"883423532389192164791648750360308884807550341691627752275345424702807307");

		int h = 1;

		ECParameterSpec ecSpec = new ECParameterSpec(curve, G, n, h);

		Security.addProvider(new BouncyCastleProvider());

		ConfigurableProvider config = (ConfigurableProvider) Security
				.getProvider("BC");

		config.setParameter(ConfigurableProvider.EC_IMPLICITLY_CA, ecSpec);

		KeyPairGenerator g = KeyPairGenerator.getInstance("ECGOST3410", "BC");
		
		g.initialize(null, new SecureRandom());

		KeyPair p = g.generateKeyPair();

		ECPrivateKey sKey = (ECPrivateKey) p.getPrivate();
		ECPublicKey vKey = (ECPublicKey) p.getPublic();
		System.out.println(sKey.toString());
		System.out.println(vKey.toString());

	}
}
